package com.kung.qvs.ui.main.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.Navigation;

import com.codbking.widget.DatePickDialog;
import com.codbking.widget.OnSureLisener;
import com.codbking.widget.bean.DateType;
import com.kung.qvs.BR;
import com.kung.qvs.R;
import com.kung.qvs.app.AppViewModelFactory;
import com.kung.qvs.databinding.FragWeighStatisticsTotalBinding;
import com.kung.qvs.utils.DateUtils;

import java.util.Date;

import me.goldze.mvvmhabit.base.BaseFragment;

public class StatisticTotalFrag extends BaseFragment<FragWeighStatisticsTotalBinding, StatisticTotalModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.frag_weigh_statistics_total;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public StatisticTotalModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(getActivity(), factory).get(StatisticTotalModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.doGetWeightStatisticTotal();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.eventToBack.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                //返回上一级
                Navigation.findNavController(getView()).navigateUp();
            }
        });

        viewModel.uc.eventDateStart.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                DatePickDialog dialog = new DatePickDialog(getContext());
                //设置上下年分限制
                dialog.setYearLimt(5);
                //设置标题
                dialog.setTitle("选择开始时间");
                //设置类型
                dialog.setType(DateType.TYPE_YMD);
                //设置消息体的显示格式，日期格式
                dialog.setMessageFormat("yyyy-MM-dd HH:mm");
                //设置选择回调
                dialog.setOnChangeLisener(null);
                //设置点击确定按钮回调
                dialog.setOnSureLisener(new OnSureLisener() {
                    @Override
                    public void onSure(Date date) {
                        viewModel.mStartDate.set(DateUtils.dataToStringDate(date));
                    }
                });
                dialog.show();
            }
        });

        viewModel.uc.eventDateEnd.observe(this, new Observer() {
            @Override
            public void onChanged(Object o) {
                DatePickDialog dialog = new DatePickDialog(getContext());
                //设置上下年分限制
                dialog.setYearLimt(5);
                //设置标题
                dialog.setTitle("选择结束时间");
                //设置类型
                dialog.setType(DateType.TYPE_YMD);
                //设置消息体的显示格式，日期格式
                dialog.setMessageFormat("yyyy-MM-dd HH:mm");
                //设置选择回调
                dialog.setOnChangeLisener(null);
                //设置点击确定按钮回调
                dialog.setOnSureLisener(new OnSureLisener() {
                    @Override
                    public void onSure(Date date) {
                        viewModel.mEndDate.set(DateUtils.dataToStringDate(date));
                    }
                });
                dialog.show();
            }
        });
    }
}
